package com.hdaccp.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.hdaccp.dao.CommonDao;
import com.hdaccp.dao.CtimeDao;
import com.hdaccp.dao.FuzerenDao;
import com.hdaccp.dao.ShejishiDao;
import com.hdaccp.dao.UdetailsDao;
import com.hdaccp.dao.ZongDao;
import com.hdaccp.dao.impl.CtimeDaoimpl;
import com.hdaccp.dao.impl.FuzerenDaoImpl;
import com.hdaccp.dao.impl.ShejishiDaoImpl;
import com.hdaccp.dao.impl.UdetailsDaoimpl;
import com.hdaccp.dao.impl.ZongDaoimpl;
import com.hdaccp.entity.Ctime;
import com.hdaccp.entity.Fuzeren;
import com.hdaccp.entity.Shejishi;
import com.hdaccp.entity.Udetails;
import com.hdaccp.entity.Zong;
@WebServlet(urlPatterns="/asdf.do")
public class asdf extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String met = request.getParameter("met");
		if(met.equals("query")){
			query(request, response);
		}else if(met.equals("bupda")){
			bupda(request, response);
		}else if(met.equals("shigongcha")){
			shigongcha(request, response);
		}else if(met.equals("shigonggai")){
			shigonggai(request, response);
		}else if(met.equals("baoinserts")){
			baoinserts(request, response);
		}else if(met.equals("query2")){
			query2(request, response);
		}else if(met.equals("fuzerencha")){
			fuzerencha(request, response);
		}else if(met.equals("fuzerengai")){
			fuzerengai(request, response);
		}else if(met.equals("shejishicha")){
			shejishicha(request, response);
		}else if(met.equals("shiejishigai")){
			shiejishigai(request, response);
		}
	}
	public void shejishicha(HttpServletRequest r, HttpServletResponse response) throws ServletException, IOException {
		r.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int zid = Integer.parseInt(r.getParameter("zid"));
		ShejishiDao sd = new ShejishiDaoImpl();
		Shejishi s = sd.getAll(zid);
		Gson g = new Gson();
		PrintWriter out = response.getWriter();
		out.print(g.toJson(s));
		out.close();
	}
	public void shiejishigai(HttpServletRequest r, HttpServletResponse response) throws ServletException, IOException {
		r.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		ShejishiDao sd = new ShejishiDaoImpl();
		Shejishi s = new Shejishi();
		s.setZid(Integer.parseInt(r.getParameter("zid")));
		s.setName(r.getParameter("name"));
		s.setPhone(r.getParameter("phone"));
		if(sd.update(s)){
			riqi rr = new riqi();
			rr.update(r, response, s.getZid());
			query(r, response);
		}
	}
	public void fuzerencha(HttpServletRequest r, HttpServletResponse response) throws ServletException, IOException {
		r.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int zid = Integer.parseInt(r.getParameter("zid"));
		FuzerenDao fd = new FuzerenDaoImpl();
		Fuzeren f = fd.getAll(zid);
		Gson g = new Gson();
		PrintWriter out = response.getWriter();
		out.print(g.toJson(f));
		out.close();
	}
	public void fuzerengai(HttpServletRequest r, HttpServletResponse response) throws ServletException, IOException {
		r.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		FuzerenDao fd = new FuzerenDaoImpl();
		Fuzeren f = new Fuzeren();
		f.setZid(Integer.parseInt(r.getParameter("zid")));
		f.setName(r.getParameter("name"));
		f.setPhone(r.getParameter("phone"));
		if(fd.bupdate(f)){
			riqi rr = new riqi();
			rr.update(r, response, f.getZid());
			query(r, response);
		}
	}
	public void baoinserts(HttpServletRequest r, HttpServletResponse response) throws ServletException, IOException {
		r.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		ZongDao zd = new ZongDaoimpl();
		Zong z = new Zong();
		z.setCname(r.getParameter("cname"));
		z.setUname(r.getParameter("uname"));
		z.setHname(r.getParameter("hname"));
		z.setDname(r.getParameter("dname"));
		z.setBdtime(r.getParameter("bdtime"));
		z.setSdtime(r.getParameter("sdtime"));
		if(zd.baoinserts(z)){
			query(r, response);
		}
	}
	public void shigongcha(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		CtimeDao cd = new CtimeDaoimpl();
		Ctime c = new Ctime();
		int id = Integer.parseInt(request.getParameter("id"));
		c = cd.getBy(id);
		Gson gson = new Gson();
		PrintWriter out = response.getWriter();
		out.print(gson.toJson(c));
		out.close();
	}
	public void shigonggai(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		CtimeDao cd = new CtimeDaoimpl();
		Ctime c = new Ctime();
		String btime = request.getParameter("btime");
		int id = Integer.parseInt(request.getParameter("id"));
		String stime = request.getParameter("stime");
		String dtime = request.getParameter("dtime");
		c.setZid(id);
		c.setBtime(btime);
		c.setDtime(dtime);
		c.setStime(stime);
		if(cd.bupd(c)){
			riqi r = new riqi();
			ZongDao zd = new ZongDaoimpl();
			zd.baojiahua(btime, stime, id);
			r.update(request, response,c.getZid());
			query(request, response);
		}
	}
	public void bupda(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		UdetailsDao dao = new UdetailsDaoimpl();
		Udetails u = new Udetails();
		u.setZid(Integer.parseInt(request.getParameter("zid")));
		u.setName(request.getParameter("name"));
		u.setCommunity(request.getParameter("xiaoqu"));
		u.setPfm(Integer.parseInt(request.getParameter("mianji")));
		u.setPhone(request.getParameter("phone"));
		if(dao.getupd(u)){
			riqi r = new riqi();
			r.update(request, response,u.getZid());
			query(request, response);
		}
	}
	public void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		CommonDao cd = new ZongDaoimpl();
		List<Zong> list = cd.getAll();
		
		System.out.println(list.size());
		
		Gson gson = new Gson();
		PrintWriter out = response.getWriter();
		out.print(gson.toJson(list));
		out.close();
	}
	
	public void query2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		ZongDao cd = new ZongDaoimpl();
		List<Zong> list = cd.sel();
		Gson gson = new Gson();
		PrintWriter out = response.getWriter();
		out.print(gson.toJson(list));
		out.close();
	}
	
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		doGet(request, response);
	}

}
